﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CSF.Utility.Mvc;
using CSF.Service.Accounts.Client;
using CSF.Security.Data;
using CSF.Utility.Misc;
using CSF.WebApp.Admin.Areas.Customers.Models;
using CSF.Customers.Data;



namespace CSF.WebApp.Admin.Areas.Customers.Controllers
{
    public class CustomerUserAccountController : SecureController
    {
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 用户列表
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Index(bool isDisable = false, int cid = 0)
        {

            UserAccountSvcClient svc = new UserAccountSvcClient();
            IEnumerable<UserAccount> list = null;
            if (isDisable)
            {
                list = svc.GetAll().Where(p => p.IsStaff == false);
            }
            else
            {
                list = svc.GetAll().Where(p => p.IsStaff == false);
            }
            //查看Customer下的UserAccount
            if (cid > 0)
            {
                CustomerSvcClient csvc = new CustomerSvcClient();
                list = csvc.GetCustomerUsers(cid);
            }
            list = Search(list);
            return View(list);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 用户列表搜索
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [NonAction]
        private IEnumerable<UserAccount> Search(IEnumerable<UserAccount> list)
        {
            var collection = Request.QueryString;

            var username = collection["UserName"];
            var email = collection["Email"];

            if (!string.IsNullOrWhiteSpace(username))
            {
                list = list.Where(p => username.InStr(new string[] { p.UserName, p.FirstName, p.LastName }));
            }
            if (!string.IsNullOrWhiteSpace(email))
            {
                list = list.Where(p => email.InStr(new string[] { p.Email }));
            }
            return list;
        }
        [CsfAuthorize]
        public ActionResult Add(int cid = 0)
        {

            CustomerSvcClient svc = new CustomerSvcClient();
            ViewData["CustomerList"] = new SelectList(svc.GetEnable(), "Id", "Code");
            CustomerUserAccountModels model = new CustomerUserAccountModels();
            if (cid > 0)
            {              
                model.CustomerId = cid;
            }
            return View(model);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 新增用户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Add(CustomerUserAccountModels model, IEnumerable<int> checkedroles)
        {
            if (ModelState.IsValid)
            {
                model.CreateBy = User.Identity.Name;
                CustomerSvcClient csvc = new CustomerSvcClient();

                int userid = csvc.InsertCustomerUser(model.ShadowCopy<UserAccount, CustomerUserAccountModels>(), model.CustomerId, User.Identity.Name);
                UserAccountSvcClient svc = new UserAccountSvcClient();
                if (checkedroles != null && checkedroles.Count() > 0)
                {
                    svc.SetUserRole(userid, checkedroles, User.Identity.Name);
                }

                return RedirectToAction("Index");
            }
            return View(model);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 编辑用户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {

            UserAccountSvcClient usvc = new UserAccountSvcClient();
            var user = usvc.GetOneById(id);
            CustomerSvcClient svccus = new CustomerSvcClient();
            IEnumerable<Customer> customerList = svccus.GetByUser(id);
            CustomerUserAccountModels CUAModel = user.ShadowCopy<UserAccount, CustomerUserAccountModels>();
            if (customerList.FirstOrDefault() != null)
            {
                CUAModel.CustomerId = customerList.FirstOrDefault().Id;
            }
            ViewData["CustomerList"] = new SelectList(svccus.GetEnable(), "Id", "Code");
            return View(CUAModel);

        }

        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 编辑用户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Edit(CustomerUserAccountModels model, IEnumerable<int> checkedroles)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = User.Identity.Name;
                CustomerSvcClient csvc = new CustomerSvcClient();
                csvc.UpdateByIdByUser(model.ShadowCopy<UserAccount, CustomerUserAccountModels>(), model.CustomerId, User.Identity.Name);

                UserAccountSvcClient svc = new UserAccountSvcClient();
                if (checkedroles != null && checkedroles.Count() > 0)
                {
                    svc.SetUserRole(model.Id, checkedroles, User.Identity.Name);
                }
                return RedirectToAction("Index");
            }
            return View(model);
        }

        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 删除用户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Delete(IEnumerable<int> id, string toUrl)
        {
            if (id != null && id.Count() > 0)
            {
                UserAccountSvcClient svc = new UserAccountSvcClient();
                svc.DeleteById(id);
            }
            return Redirect(toUrl);
        }


        /// <summary>
        /// author:andy-peng
        /// date:2011-08-30
        /// 用户角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult UserAccountRoleControl(int id = 0)
        {
            //返回所有员工的角色列表
            UserRoleSvcClient svcur = new UserRoleSvcClient();
            IEnumerable<UserRole> model;
            model = svcur.GetAll().Where(p => p.IsStaff == false);

            //根据用户ID，返回用户的角色
            UserAccountSvcClient srcua = new UserAccountSvcClient();
            List<int> Accountrole = new List<int>();
            if (id > 0)
            {
                Accountrole = srcua.GetUserRoles(id).ToList();
            }

            //返回已授权的用户角色列表
            List<RoleDistribution> distri = new List<RoleDistribution>();
            foreach (UserRole user in model)
            {
                RoleDistribution roledistri = new RoleDistribution();
                bool condition = false;
                foreach (int role in Accountrole)
                {
                    if (role == user.Id)
                        condition = true;
                }
                roledistri.Id = user.Id;
                roledistri.Code = user.Code;
                roledistri.IsCheckCode = condition;
                roledistri.Name1 = user.Name1;
                distri.Add(roledistri);
            }

            return View(distri);
        }

    }
}
